﻿/// <reference path="jquery/jquery-1.5.1.js" />
/// <reference path="SkyKnowledge.js" />
/*  Last updated: 2012/06/17 18:00  */

var OOPager = function (wrapersSelector, resultsSelector) {
    this.WrapersSelector = wrapersSelector;
    this.ResultsSelector = resultsSelector;
    this.Total = 0;
    this.PageSize = 15;
    this.PageIndex = 0;
    this.ShowingPageLinks = 10;
    this.InfoFormat = "Displaying " + OOPager.Tokens.FromItemNumber + " - " + OOPager.Tokens.ToItemNumber + " items/" + OOPager.Tokens.TotalItemCount + " items";
    this.RenderPagerOnClick = true;
    this.ShowingButtons = 10;
};

OOPager.prototype.Init = function () {
    this.PageTo(0);
};

OOPager.prototype.GetTotalPages = function () {
    return Math.ceil(this.Total / this.PageSize);
};

OOPager.prototype.HasPrevious = function () {
    return this.PageIndex > 0;
};

OOPager.prototype.HasNext = function () {
    var total = this.GetTotalPages();
    return this.PageIndex < total - 1;
};

OOPager.prototype.Render = function () {
    var pager = this;
    var ul = "<ul>";
    ul += "<li class='first" + (pager.HasPrevious() ? "" : " disabled") + "'><a href='javascript:void(0)'></a></li>";
    ul += "<li class='previous" + (pager.HasPrevious() ? "" : " disabled") + "'><a href='javascript:void(0)'></a></li>";
    var start = pager.GetStartPageNumber();
    if (start < 1) {
        start = 1;
    }
    var total = pager.GetTotalPages();
    var end = start + pager.ShowingButtons - 1;
    if (end > total) {
        end = total;
    }
    for (var i = start; i <= end; i++) {
        if (i == start && start > 1) {
            ul += "<li class='number start'><a pager-index='" + (start - 1) + "' href='javascript:void(0)'>...</a></li>";
        }

        var startEndClass = "";
        if (i == start && start <= 1) {
            startEndClass += " start";
        }
        if (i == end && end >= total) {
            startEndClass += " end";
        }
        if (i - 1 == pager.PageIndex) {
            ul += "<li class='number current" + startEndClass + "'><a pager-index='" + i + "' href='javascript:void(0)'>" + i + "</a></li>";
        } else {
            ul += "<li class='number" + startEndClass + "'><a pager-index='" + i + "' href='javascript:void(0)'>" + i + "</a></li>";
        }
        if (i == end && end < total) {
            ul += "<li class='number end'><a pager-index='" + (end + 1) + "' href='javascript:void(0)'>...</a></li>";
        }
    }
    ul += "<li class='next" + (pager.HasNext() ? "" : " disabled") + "'><a href='javascript:void(0)'></a></li>";
    ul += "<li class='last" + (pager.HasNext() ? "" : " disabled") + "'><a href='javascript:void(0)'></a></li>";
    ul += "</ul>";
    var infoHtml = "<div class='pager-info'>";
    var format = pager.InfoFormat + "";
    format = format.replace(OOPager.Tokens.CurrentPageNumber, pager.PageIndex + 1);
    var fromItemNumber = (pager.PageIndex * pager.PageSize) + 1;
    if (fromItemNumber < 0) {
        fromItemNumber = 0;
    }
    format = format.replace(OOPager.Tokens.FromItemNumber, fromItemNumber);
    format = format.replace(OOPager.Tokens.PageSize, pager.PageSize);
    var toItemNumber = (pager.PageIndex + 1) * pager.PageSize;
    if (toItemNumber > pager.Total) {
        toItemNumber = pager.Total;
    }
    format = format.replace(OOPager.Tokens.ToItemNumber, toItemNumber);
    format = format.replace(OOPager.Tokens.TotalItemCount, pager.Total);
    infoHtml += format + "</div>";
    $(pager.WrapersSelector).each(function () {
        var $wrapper = $(this);
        $wrapper.empty();
        $wrapper.addClass("pager");
        $wrapper.append(ul);
    });
    $(pager.ResultsSelector).html(infoHtml);

    $(pager.WrapersSelector).find("li.first:not(.disabled) a").click(function () {
        if (pager.RenderPagerOnClick) {
            pager.PageTo(0);
        }
        pager.TriggerPageClicked(0);
    });
    $(pager.WrapersSelector).find("li.previous:not(.disabled) a").click(function () {
        if (pager.RenderPagerOnClick) {
            pager.PageTo(pager.PageIndex - 1);
        }
        pager.TriggerPageClicked(pager.PageIndex - 1);
    });
    $(pager.WrapersSelector).find("li.next:not(.disabled) a").click(function () {
        if (pager.RenderPagerOnClick) {
            pager.PageTo(pager.PageIndex + 1);
        }
        pager.TriggerPageClicked(pager.PageIndex + 1);
    });
    $(pager.WrapersSelector).find("li.last:not(.disabled) a").click(function () {
        if (pager.RenderPagerOnClick) {
            pager.PageTo(pager.GetTotalPages() - 1);
        }
        pager.TriggerPageClicked(pager.GetTotalPages() - 1);
    });
    $(pager.WrapersSelector).find("li.number:not(.current) a").click(function () {
        var newIndex = $(this).attr("pager-index") * 1 - 1;
        if (pager.RenderPagerOnClick) {
            pager.PageTo(newIndex);
        }
        pager.TriggerPageClicked(newIndex);
    });
};

OOPager.prototype.PageTo = function (index) {
    var maxIndex = this.GetTotalPages() - 1;
    if (index > maxIndex) {
        index = maxIndex;
    }
    this.PageIndex = index;
    this.Render();
};

OOPager.prototype.GetStartPageNumber = function () {
    var isTimes = (this.PageIndex + 1) % this.ShowingButtons == 0;
    if (isTimes) {
        return this.PageIndex + 2 - this.ShowingButtons;
    }
    return Math.floor((this.PageIndex + 1) / this.ShowingButtons) * this.ShowingButtons + 1;
};

OOPager.prototype.TriggerPageClicked = function (toIndex) {
    var evt = $.Event("PageClicked");
    evt.ToIndex = toIndex;
    $(this).trigger(evt);
};

OOPager.prototype.OnPageClicked = function (func) {
    $(this).bind("PageClicked", function (evt) {
        func(evt.ToIndex);
    });
};

OOPager.Tokens = {
    TotalItemCount: "{TotalItemCount}",
    TotalPageCount: "{TotalPageCount}",
    CurrentPageNumber: "{CurrentPageNumber}",
    PageSize: "{PageSize}",
    FromItemNumber: "{FromItemNumber}",
    ToItemNumber: "{ToItemNumber}"
};